package likou;

import java.util.*;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/4/9  8:29
 */
public class _1 {
    public static void main(String[] args) {
        int xx[] = {3,2,4};
        int sad[] = twoSum2(xx, 6);
        for (int i = 0; i < sad.length; i++) {
            System.out.println(sad[i]);
        }
    }

    public static int[] twoSum(int[] nums, int target) {
        int xx[] = new int[0];
        int x = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    xx = new int[xx.length + 2];
                    xx[x] = i;
                    x++;
                    xx[x] = j;
                    return xx;
                }
            }
        }
        return xx;
    }

    public static int[] twoSum2(int[] nums, int target) {
        int xx[] = new int[0];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int daan = target - nums[i];
            if (map.containsKey(daan)&&map.get(daan)!=i){
                xx = new int[2];
                xx[0]=i;
                xx[1]=map.get(daan);
                return xx;
            }
        }
        return xx;
    }
}
